//
//  AZUserViewModel.swift
//  sinaWB
//
//  Created by AzeryKiss on 16/2/26.
//  Copyright © 2016年 AzeryKiss. All rights reserved.
//

import UIKit
import AFNetworking

class AZUserViewModel: NSObject {
    
//     var shareInstance = AZUserViewModel()
    
//    var access_token: String?{
//        return userAccount?.access_token
//    }
    
    var userAccount: AZUserModel?
    
    override init() {
        userAccount = AZUserModel.decodeData("data.data")
    }
    
    
    var isLogin: Bool{
        if userAccount?.access_token != nil {
                return true
        } else {
            return false
        }
      
//        if  userAccount?.access_token?.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) > 0 {
//            return true
//        } else {
//            return false
//        }
//        return false
    }

    func loadToken(code: String, sucess: () -> (), faulse: () -> ()) {
        
        let url = "https://api.weibo.com/oauth2/access_token"
        
        let dict = ["client_id" : APP_KEY,
                        "client_secret" : APP_SECRET,
            "grant_type" : "authorization_code",
            "code" : code,
            "redirect_uri" : APP_URL]
            
            let afn = AFHTTPSessionManager()
        
        afn.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        afn.POST(url, parameters: dict, progress: nil, success: { (_, responseData) -> Void in
//            print(responseData)\
            if let response = responseData {
                
               let userM =  AZUserModel(dict: (response as? [String : NSObject])!)
//                loadToken(userM)
                self.loadUserInfo(userM, sucess: sucess  , faulse: faulse)

                userM.save("data.data")
            }

            }) { (_, error) -> Void in
                print(error)
        }
        
    }
    
    
    
    func loadUserInfo(userM: AZUserModel, sucess: () -> (), faulse: () -> ()) {
        let url = "https://api.weibo.com/2/users/show.json"
        let afnManager = AFHTTPSessionManager()
        
        var params = [String : NSObject]()
        
        params = ["access_token" : userM.access_token ?? "",
            "uid" : userM.uid ?? ""]
        
                afnManager.GET(url, parameters: params, progress: nil, success: { (_, responseData) -> Void in

//                    responseData as? [String: NSObject]
                    
//                    if  let dict = responseData as? [String : NSObject]{
                    
                    if let response = responseData as? [String : AnyObject] {
                        
                        let cc = AZUserModelTrue(dict: response)
                        
                        cc.saveData()
                    
//                        let dict = response as! [String : NSObject]
                        
                        
                        
//                        userM.profile_image_url = dict["profile_image_url"] as? String
//                        
//                        userM.screen_name = dict["screen_name"] as? String

                        
//                        let userM = AZUserModel(dict: response as! [String : NSObject] )
//                        userM.save("data.data")
                        
//                        print(userM)
                        sucess()

                    }
                    
                    
                    }) { (_, error) -> Void in
                        print(error)
                        faulse()
        }
        
        
    }
    
}
